package com.balabala.gym_system.controller.admin;

import com.balabala.gym_system.pojo.dto.BaseDeleteDto;
import com.balabala.gym_system.pojo.dto.NoticeDto;
import com.balabala.gym_system.pojo.entity.Notice;
import com.balabala.gym_system.pojo.vo.NoticeVo;
import com.balabala.gym_system.service.NoticeService;
import com.balabala.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/admin/notice")
@CrossOrigin
@Api(tags = "公告管理相关接口")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping(value = "/list")
    @ApiOperation("分页查询公告")
    public Result list(NoticeDto noticeDto) throws ParseException {
       PageHelper.startPage(noticeDto.getPage(),noticeDto.getLimit());
       List<NoticeVo> noticeByTitleAndDate = noticeService.findNoticeByTitleAndDate(noticeDto);
       PageInfo<NoticeVo> noticePageInfo = new PageInfo<>(noticeByTitleAndDate);
       return Result.ok(noticeByTitleAndDate,noticePageInfo.getTotal());
    }


    @GetMapping(value = "/findAll")
    @ApiOperation("显示所有公告")
    public Result findAll(){
         List<Notice> list = noticeService.list();
         return Result.ok(list);
    }

    @PostMapping(value = "/findById/{id}")
    @ApiOperation("根据id查询公告")
    public Result findById(@PathVariable("id") Long id){
        Notice notice = noticeService.getById(id);
        return Result.ok(notice);
    }

    @PostMapping(value = "/add")
    @ApiOperation("发布公告")
    public Result add(@RequestBody Notice notice){
         boolean save = noticeService.save(notice);
         return Result.ok();
    }

    @PostMapping(value = "/update")
    @ApiOperation("修改公告")
    public Result update(@RequestBody Notice notice){
        boolean save = noticeService.updateById(notice);
        return Result.ok();
    }

    @PostMapping(value = "/deleteById")
    @ApiOperation("根据id删除公告")
    public Result deleteById(@RequestParam Long id){
        boolean save= noticeService.removeById(id);
         return Result.ok();
    }


    @PostMapping(value = "/deleteByIds")
    @ApiOperation("批量删除公告")
    public Result deleteByIds(@RequestBody BaseDeleteDto baseDeleteDto){
        boolean b = noticeService.removeByIds(baseDeleteDto.getIds());
        return Result.ok();
    }
}
